package com.mz.mattress.client.util;

import cn.binarywang.wx.miniapp.api.WxMaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Base64;

@Component
@Slf4j
public class WxQrcodeUtil {

    @Resource(name = "clientWxMaService")
    private WxMaService clientWxMaService;

    /**
     * pages/device/deviceIndex/deviceIndex?deviceCode=67738bd8353aa6e11d5c96a3
     * @param scene
     * @param page
     * @param width
     * @return
     */
    public String generateQrcode(String scene, String page, int width) {
        String data = null;
        try {
            log.info("deviceCode=" + scene);
            byte[] fileByte = clientWxMaService.getQrcodeService().createWxaCodeUnlimitBytes(scene, page,
                    false, "release", width, false, null, false);
            data = Base64.getEncoder().encodeToString(fileByte);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("generateQrcode_error", e);
        }
        return data;
    }

    public byte[] generateQrcodeByte(String scene, String page, int width) {
        try {
            log.info("c=" + scene);
            scene = "c=" + scene;
            byte[] fileByte = clientWxMaService.getQrcodeService().createWxaCodeUnlimitBytes(scene, page,
                    false, "release", width, false, null, false);
            return fileByte;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("generateQrcode_error", e);
        }
        return null;
    }

}
